home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************************
- *
- *
- * ObjectMacZapp -- a standard Mac OOP application template
- *
- *
- *
- * ZPictWindow.cpp -- a window that displays PICT files
- *
- *
- *
- *
- *
- * © 1996, Graham Cox
- *
- *
- *
- *
- *************************************************************************************************/
-
-
- #include "ZPictWindow.h"
- #include "ZErrors.h"
- #include "ZDefines.h"
-
-
- /*--------------------------------*** CONSTRUCTOR ***---------------------------------*/
-
- ZPictWindow::ZPictWindow(ZCommander* aBoss, short windID)
- : ZScroller(aBoss, windID, TRUE, TRUE )
- {
- thePicture = NULL;
- }
-
-
- /*---------------------------------*** DESTRUCTOR ***---------------------------------*/
-
- ZPictWindow::~ZPictWindow()
- {
- if (thePicture)
- KillPicture(thePicture);
- }
-
- /*--------------------------------*** DRAWCONTENT ***---------------------------------*/
- /*
-
- overrides ZScroller to draw the picture in the window
-
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::DrawContent()
- {
- Rect pFrame;
-
- if (thePicture)
- {
- pFrame = (*thePicture)->picFrame;
- OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
- DrawPicture(thePicture, &pFrame);
- }
- }
-
-
- /*----------------------------------*** OPENFILE ***----------------------------------*/
- /*
-
- overrides the base ZWindow to open a PICT file on disk to the window
-
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::OpenFile(OSType fType)
- {
- // read the picture into the picture handle
-
- FInfo fi;
- short refNum;
- long pSize;
- Handle temp = NULL;
- Rect pFrame;
-
- if (macFile.vRefNum != kNoFile)
- {
- FailOSErr(FSpGetFInfo(&macFile, &fi));
-
- if (fi.fdType != 'PICT')
- FailOSErr(paramErr);
-
- FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum ));
-
- // skip the first 512 bytes of the file
-
- try
- {
- FailOSErr(SetFPos(refNum, fsFromStart, 512));
-
- // how big is the rest of the file?
-
- FailOSErr(GetEOF(refNum, &pSize));
- pSize -= 512;
-
- if (pSize > 0)
- {
- FailNIL(temp = NewHandle(pSize));
-
- HLock(temp);
- FailOSErr(FSRead(refNum, &pSize, *temp));
- HUnlock(temp);
- }
-
- FSClose(refNum);
-
- if (thePicture)
- KillPicture(thePicture);
-
- thePicture = (PicHandle)temp;
-
- // set the scroll dimensions to the picture's frame
-
- pFrame = (*thePicture)->picFrame;
- OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
- SetBounds(&pFrame);
- SetScrollAmount(8,8);
-
- pFrame.right += kStdScrollbarWidth;
- pFrame.bottom += kStdScrollbarWidth;
-
- SetSize(pFrame.right, pFrame.bottom);
- }
- catch(OSErr err)
- {
- FSClose(refNum);
-
- if (temp)
- {
- HUnlock(temp);
- DisposeHandle(temp);
- }
- throw err;
- }
- }
- inherited::OpenFile( fType );
- }
-
-
-
- /*--------------------------------*** SAVEFILE ***---------------------------------*/
- /*
-
- overrides the base ZWindow to save the image as a PICT file on disk
-
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::SaveFile()
- {
- // this saves the window contents as a PICT file in the file <macFile>.
-
- short refNum;
- long pSize;
- Handle header = NULL;
- OSErr theErr;
-
- if ((macFile.vRefNum != kNoFile) && (thePicture != NULL))
- {
- theErr = FSpOpenDF(&macFile, fsCurPerm, &refNum);
-
- if (theErr == fnfErr)
- {
- FailOSErr(FSpCreate(&macFile, gMacZoopSignature, 'PICT', 0));
- FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum));
- }
- else
- FailOSErr(theErr);
-
- try
- {
- // make a header and write it out. This is just 512 bytes of zeroes.
-
- FailNIL(header = NewHandleClear(pSize = 512));
-
- HLock(header);
- FailOSErr(FSWrite(refNum, &pSize, *header));
- HUnlock(header);
- DisposeHandle(header);
- header = NULL;
-
- // now write out the picture data
-
- pSize = GetHandleSize((Handle) thePicture);
-
- HLock((Handle) thePicture);
- FailOSErr(FSWrite(refNum, &pSize, *thePicture));
- HUnlock((Handle) thePicture);
-
- // set the file length to the current mark
-
- FailOSErr(GetFPos(refNum, &pSize));
- FailOSErr(SetEOF(refNum, pSize));
-
- FSClose(refNum);
- }
- catch(OSErr err)
- {
- FSClose(refNum);
-
- if (header)
- {
- HUnlock(header);
- DisposeHandle(header);
- }
-
- HUnlock((Handle) thePicture);
-
- throw err;
- }
- inherited::SaveFile();
- }
- }
-
-